package com.xing.fruit.servlet;

import com.xing.fruit.pojo.User;
import com.xing.fruit.service.user.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String phone = null;
        String email = null;
        phone = req.getParameter("phone");
        email = req.getParameter("email");
        String password = req.getParameter("pwd");
        User user = new User();
        if (phone != null){
            user.setUsername(phone);
            user.setPhone(phone);
        }
        if (email != null){
            user.setUsername(email);
            user.setEmail(email);
        }

        user.setPassword(password);
        UserServiceImpl userService = new UserServiceImpl();
        int i = userService.userReg(user);
        if (i>0){
            resp.sendRedirect("login.jsp");
        }else {
            resp.sendRedirect("register.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
